
<%@page import="org.lxy.service.InfomationService"%>
<%@page import="org.lxy.domain.InfomationEntity"%>
<%@page import="org.apache.commons.fileupload.FileUploadException"%>
<%@page import="org.lxy.domain.UserInfoEntity"%>
<%@page import="java.io.File"%>
<%@page import="java.util.Iterator"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="java.util.List"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.FileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@ page language="java" import="java.util.*"
	contentType="text/html; charset=utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%
request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		boolean isMultipart = ServletFileUpload.isMultipartContent(request);
		if (isMultipart) {
			FileItemFactory factory = new DiskFileItemFactory();
			ServletFileUpload upload = new ServletFileUpload(factory);
			try {
				List<FileItem> items = upload.parseRequest(request);
				Iterator<FileItem> it = items.iterator();
				String title = "";
				String jianjie = "";
				String neirong = "";
				int shuliang = 0;
				int id = 0;
				String fname="";
				String xiangqing="";
				while (it.hasNext()) {
					FileItem item = it.next();
					String itemname = item.getFieldName();

					if (item.isFormField()) {
						if (itemname.equals("title")) {
							title = item.getString("utf-8");
						} else if (itemname.equals("jianjie")) {
							jianjie = item.getString("utf-8");

						} else if (itemname.equals("neirong")) {
							neirong =item.getString("utf-8");
						} else if (itemname.equals("id")) {
							id = Integer.parseInt(item.getString("utf-8"));
						}  else {
							System.out.println("其他字段");
						}
					} else {
						fname = item.getName();
						System.out.println("fname=" + fname);
						String uploadpath = request.getSession().getServletContext().getRealPath("upload");
						// String path = "E:\\upload";
						System.out.println("path=" + path);
						if (!fname.isEmpty()) {
							File file = new File(uploadpath, fname);
							item.write(file);
						}
						//request.setAttribute("news", fname + "上传成功,谢谢分享");
						// request.getRequestDispatcher("print.jsp").forward(request,response);
						// System.out.println(filename+"上传成功!!!");
						// return;
					}

				}
				zixun entity = new zixun();
				zixunService service = new zixunService();
				userinfo account = (userinfo) request.getSession().getAttribute("Account");

				if (account == null) {
					out.write("<script>window.top.location.href='Login.jsp';</script>");
					return;
				}
				if (id > 0) {

					entity = service.getEntityById(id);
				} else {

					

					entity.setId(0);
					entity.setTupian("");
					entity.setZhuangtai("0");
				}
				entity.setJianjie(jianjie);
				
				entity.setNeirong(neirong);
				entity.setTitle(title);
				
				if (!fname.isEmpty()) {
					entity.setTupian(fname);
				}
				
				boolean tag = false;
				if (entity.getId() > 0) {
					tag = service.update(entity);
				} else {
					tag = service.add(entity);
				}
				if (tag) {
						out.write(
								"<script>alert('操作成功!');window.parent.location.href='/infomation/operation?op=List"
										+ "';</script>");
					
				} else {
					out.write("<script>alert('操作失败!');</script>");
					

				}
			} catch (FileUploadException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		 %>